package com.gzwanchuang.xhgj.ui.dialog;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.RadioButton;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.gzwanchuang.xhgj.R;
import com.gzwanchuang.xhgj.bean.SaItem;
import com.gzwanchuang.xhgj.ui.view.LiumRadioGroup;

import java.util.ArrayList;
import java.util.List;

import kale.ui.view.dialog.BaseCustomDialog;
import kale.ui.view.dialog.BaseEasyDialog;

/**
 * 文件名 : SelectMonthDialog
 * 创建者 : MangoWang
 * 创建日期 : 2019/12/24 12:01
 * 微信 : MangoWaWang
 * 邮箱 : 763482205@qq.com
 * 描述 : TODO
 */
public class SelectTypeDialog extends BaseCustomDialog {

    public static final String KEY_FANG = "KEY_FANG";
    public static final String KEY_STATUS = "KEY_STATUS";

    public OnDialogClickListener dialogListener;
    public List<SaItem> dialogFangLists;
    public List<SaItem> dialogStatusLists;
    public List<RadioButton> fangButtonS = new ArrayList<>();
    public List<RadioButton> StatusButtonS = new ArrayList<>();
    LiumRadioGroup rgfang;
    LiumRadioGroup rgZhuang;
    TextView tvChongZhi, tvSure;
    RadioButton radioButton1, radioButton2, radioButton3, radioButton4, radioButton5, radioButton6, radioButton7,
            radioButton8, radioButton9, radioButton10, radioButton11, radioButton12;

    RadioButton radioZhangButton1, radioZhangButton2, radioZhangButton3, radioZhangButton4,
            radioZhangButton5, radioZhangButton6, radioZhangButton7, radioZhangButton8,
            radioZhangButton9, radioZhangButton10, radioZhangButton11, radioZhangButton12,
            radioZhangButton13, radioZhangButton14, radioZhangButton15,radioZhangButton16,
            radioZhangButton17,radioZhangButton18;


    //方式    1-全部;2-激活交易;3-支付宝被扫;4-支付宝主扫;5-微信被扫;6-微信主扫;7-银联二维码;8-银联二维码主扫;9-借记卡;10-贷记卡;11-云闪付
    //状态    1-全部;2-成功;3-失败;4-被冲正;5-预记状态;6-发送失败;7-发送超时
    String mType = "1", mStatus = "1", mTypeName = "全部", mStatusName = "全部";

    /**
     * 自定义builder来增加一些参数，记得要继承自BaseEasyDialog.Builder
     */
    public static class Builder extends BaseEasyDialog.Builder<Builder> {
        private Bundle bundle = new Bundle();
        private SelectTypeDialog.OnDialogClickListener listener;
        private List<SaItem> fangLists;
        private List<SaItem> StatusLists;


        public Builder(@NonNull Context context) {
            super(context);
        }


        public SelectTypeDialog.Builder setOnDialogClickListener(SelectTypeDialog.OnDialogClickListener lis) {
            listener = lis;
            return this;
        }


        public SelectTypeDialog.Builder setOnDialogFangDate(List<SaItem> fangList) {
            fangLists = fangList;
            return this;
        }


        public SelectTypeDialog.Builder setOnDialogStatusDate(List<SaItem> StatusList) {
            StatusLists = StatusList;
            return this;
        }

        public SelectTypeDialog.Builder setFangTypeStatus(String fangType, String status) {
            bundle.putString(KEY_FANG, fangType);
            bundle.putString(KEY_STATUS, status);
            return this;
        }


        @NonNull
        @Override
        protected SelectTypeDialog createDialog() {
            SelectTypeDialog dialog = new SelectTypeDialog();
            dialog.setArguments(bundle);
            dialog.dialogListener = listener;
            dialog.dialogFangLists = fangLists;
            dialog.dialogStatusLists = StatusLists;
            return dialog;
        }
    }


    @Override
    protected int getLayoutResId() {
        return R.layout.dialog_select_shou_type;
    }

    @Override
    protected void bindViews(View view) {
        rgfang = view.findViewById(R.id.rg_fang);
        rgZhuang = view.findViewById(R.id.rg_zhuang);
        tvChongZhi = view.findViewById(R.id.tv_chong_zhi);
        tvSure = view.findViewById(R.id.tv_sure);
        radioButton1 = view.findViewById(R.id.rb_fang_one);
        radioButton2 = view.findViewById(R.id.rb_fang_two);
        radioButton3 = view.findViewById(R.id.rb_fang_three);
        radioButton4 = view.findViewById(R.id.rb_fang_four);
        radioButton5 = view.findViewById(R.id.rb_fang_five);
        radioButton6 = view.findViewById(R.id.rb_fang_six);
        radioButton7 = view.findViewById(R.id.rb_fang_seven);
        radioButton8 = view.findViewById(R.id.rb_fang_eight);
        radioButton9 = view.findViewById(R.id.rb_fang_nine);
        radioButton10 = view.findViewById(R.id.rb_fang_ten);
        radioButton11 = view.findViewById(R.id.rb_fang_eleven);
        radioButton12 = view.findViewById(R.id.rb_fang_twelve);
        fangButtonS.add(radioButton1);
        fangButtonS.add(radioButton2);
        fangButtonS.add(radioButton3);
        fangButtonS.add(radioButton4);
        fangButtonS.add(radioButton5);
        fangButtonS.add(radioButton6);
        fangButtonS.add(radioButton7);
        fangButtonS.add(radioButton8);
        fangButtonS.add(radioButton9);
        fangButtonS.add(radioButton10);
        fangButtonS.add(radioButton11);
        fangButtonS.add(radioButton12);


        radioZhangButton1 = view.findViewById(R.id.rb_zhuang_one);
        radioZhangButton2 = view.findViewById(R.id.rb_zhuang_two);
        radioZhangButton3 = view.findViewById(R.id.rb_zhuang_three);
        radioZhangButton4 = view.findViewById(R.id.rb_zhuang_four);
        radioZhangButton5 = view.findViewById(R.id.rb_zhuang_five);
        radioZhangButton6 = view.findViewById(R.id.rb_zhuang_six);
        radioZhangButton7 = view.findViewById(R.id.rb_zhuang_seven);
        radioZhangButton8 = view.findViewById(R.id.rb_zhuang_eight);
        radioZhangButton9 = view.findViewById(R.id.rb_zhuang_nine);
        radioZhangButton10 = view.findViewById(R.id.rb_zhuang_ten);
        radioZhangButton11 = view.findViewById(R.id.rb_zhuang_eleven);
        radioZhangButton12 = view.findViewById(R.id.rb_zhuang_twelve);
        radioZhangButton13 = view.findViewById(R.id.rb_zhuang_shisan);
        radioZhangButton14 = view.findViewById(R.id.rb_zhuang_shishi);
        radioZhangButton15 = view.findViewById(R.id.rb_zhuang_shiwu);
        radioZhangButton16 = view.findViewById(R.id.rb_zhuang_shiliu);
        radioZhangButton17 = view.findViewById(R.id.rb_zhuang_shiqi);
        radioZhangButton18 = view.findViewById(R.id.rb_zhuang_shiba);

        StatusButtonS.add(radioZhangButton1);
        StatusButtonS.add(radioZhangButton2);
        StatusButtonS.add(radioZhangButton3);
        StatusButtonS.add(radioZhangButton4);
        StatusButtonS.add(radioZhangButton5);
        StatusButtonS.add(radioZhangButton6);
        StatusButtonS.add(radioZhangButton7);
        StatusButtonS.add(radioZhangButton8);
        StatusButtonS.add(radioZhangButton9);
        StatusButtonS.add(radioZhangButton10);
        StatusButtonS.add(radioZhangButton11);
        StatusButtonS.add(radioZhangButton12);
        StatusButtonS.add(radioZhangButton13);
        StatusButtonS.add(radioZhangButton14);
        StatusButtonS.add(radioZhangButton15);
        StatusButtonS.add(radioZhangButton16);
        StatusButtonS.add(radioZhangButton17);
        StatusButtonS.add(radioZhangButton18);


        mType = getArguments().getString(KEY_FANG);
        mStatus = getArguments().getString(KEY_STATUS);


        // 数据传入 判断有多少个
        for (int i = 0; i < dialogFangLists.size(); i++) {
            fangButtonS.get(i).setText(dialogFangLists.get(i).getTitle());
            fangButtonS.get(i).setTag(dialogFangLists.get(i).getCode());
            fangButtonS.get(i).setVisibility(View.VISIBLE);
            if (dialogFangLists.get(i).getCode().equals(mType)) {
                mTypeName = dialogFangLists.get(i).getTitle();
                fangButtonS.get(i).setChecked(true);
            }
        }
        // 数据传入 判断有多少个
        int len = 18;
        Log.e("dialogStatusLists", "bindViews: "+ dialogStatusLists.size() );
        if (dialogStatusLists.size() > len) {
            len = 18;
        } else {
            len = dialogStatusLists.size();
        }
        for (int i = 0; i < len; i++) {
            StatusButtonS.get(i).setText(dialogStatusLists.get(i).getTitle());
            StatusButtonS.get(i).setTag(dialogStatusLists.get(i).getCode());
            StatusButtonS.get(i).setVisibility(View.VISIBLE);
            if (dialogStatusLists.get(i).getCode().equals(mStatus)) {
                mStatusName = dialogStatusLists.get(i).getTitle();
                StatusButtonS.get(i).setChecked(true);
            }
        }
        setListener();
    }

    private void setListener() {
        rgfang.setOnCheckedChangeListener((group, checkedId) -> {
            switch (checkedId) {
                case R.id.rb_fang_one:
                    mType = radioButton1.getTag().toString();
                    mTypeName = radioButton1.getText().toString();
                    break;
                case R.id.rb_fang_two:
                    mType = radioButton2.getTag().toString();
                    mTypeName = radioButton2.getText().toString();
                    break;
                case R.id.rb_fang_three:
                    mType = radioButton3.getTag().toString();
                    mTypeName = radioButton3.getText().toString();
                    break;
                case R.id.rb_fang_four:
                    mType = radioButton4.getTag().toString();
                    mTypeName = radioButton4.getText().toString();
                    break;
                case R.id.rb_fang_five:
                    mType = radioButton5.getTag().toString();
                    mTypeName = radioButton5.getText().toString();
                    break;
                case R.id.rb_fang_six:
                    mType = radioButton6.getTag().toString();
                    mTypeName = radioButton6.getText().toString();
                    break;
                case R.id.rb_fang_seven:
                    mType = radioButton7.getTag().toString();
                    mTypeName = radioButton7.getText().toString();
                    break;
                case R.id.rb_fang_eight:
                    mType = radioButton8.getTag().toString();
                    mTypeName = radioButton8.getText().toString();
                    break;
                case R.id.rb_fang_nine:
                    mType = radioButton9.getTag().toString();
                    mTypeName = radioButton9.getText().toString();
                    break;
                case R.id.rb_fang_ten:
                    mType = radioButton10.getTag().toString();
                    mTypeName = radioButton10.getText().toString();
                    break;
                case R.id.rb_fang_eleven:
                    mType = radioButton11.getTag().toString();
                    mTypeName = radioButton11.getText().toString();
                    break;
                case R.id.rb_fang_twelve:
                    mType = radioButton12.getTag().toString();
                    mTypeName = radioButton12.getText().toString();
                    break;

            }
            Log.e("adadd", mType + mTypeName);
        });
        rgZhuang.setOnCheckedChangeListener((group, checkedId) -> {
            switch (checkedId) {
                case R.id.rb_zhuang_one:
                    mStatus = radioZhangButton1.getTag().toString();
                    mStatusName = radioZhangButton1.getText().toString();
                    break;
                case R.id.rb_zhuang_two:
                    mStatus = radioZhangButton2.getTag().toString();
                    mStatusName = radioZhangButton2.getText().toString();
                    break;
                case R.id.rb_zhuang_three:
                    mStatus = radioZhangButton3.getTag().toString();
                    mStatusName = radioZhangButton3.getText().toString();
                    break;
                case R.id.rb_zhuang_four:
                    mStatus = radioZhangButton4.getTag().toString();
                    mStatusName = radioZhangButton4.getText().toString();
                    break;
                case R.id.rb_zhuang_five:
                    mStatus = radioZhangButton5.getTag().toString();
                    mStatusName = radioZhangButton5.getText().toString();
                    break;
                case R.id.rb_zhuang_six:
                    mStatus = radioZhangButton6.getTag().toString();
                    mStatusName = radioZhangButton6.getText().toString();
                    break;
                case R.id.rb_zhuang_seven:
                    mStatus = radioZhangButton7.getTag().toString();
                    mStatusName = radioZhangButton7.getText().toString();
                    break;
                case R.id.rb_zhuang_eight:
                    mStatus = radioZhangButton8.getTag().toString();
                    mStatusName = radioZhangButton8.getText().toString();
                    break;
                case R.id.rb_zhuang_nine:
                    mStatus = radioZhangButton9.getTag().toString();
                    mStatusName = radioZhangButton9.getText().toString();
                    break;
                case R.id.rb_zhuang_ten:
                    mStatus = radioZhangButton10.getTag().toString();
                    mStatusName = radioZhangButton10.getText().toString();
                    break;
                case R.id.rb_zhuang_eleven:
                    mStatus = radioZhangButton11.getTag().toString();
                    mStatusName = radioZhangButton11.getText().toString();
                    break;
                case R.id.rb_zhuang_twelve:
                    mStatus = radioZhangButton12.getTag().toString();
                    mStatusName = radioZhangButton12.getText().toString();
                    break;
                case R.id.rb_zhuang_shisan:
                    mStatus = radioZhangButton13.getTag().toString();
                    mStatusName = radioZhangButton13.getText().toString();
                    break;
                case R.id.rb_zhuang_shishi:
                    mStatus = radioZhangButton14.getTag().toString();
                    mStatusName = radioZhangButton14.getText().toString();
                    break;
                case R.id.rb_zhuang_shiwu:
                    mStatus = radioZhangButton15.getTag().toString();
                    mStatusName = radioZhangButton15.getText().toString();
                    break;
                case R.id.rb_zhuang_shiliu:
                    mStatus = radioZhangButton16.getTag().toString();
                    mStatusName = radioZhangButton16.getText().toString();
                    break;
                case R.id.rb_zhuang_shiqi:
                    mStatus = radioZhangButton17.getTag().toString();
                    mStatusName = radioZhangButton17.getText().toString();
                    break;
                case R.id.rb_zhuang_shiba:
                    mStatus = radioZhangButton18.getTag().toString();
                    mStatusName = radioZhangButton18.getText().toString();
                    break;

            }
            Log.e("adadd", mStatus + mStatusName);
        });

        tvChongZhi.setOnClickListener(v -> {
            rgZhuang.clearCheck();
            rgfang.clearCheck();
            mType = fangButtonS.get(0).getTag().toString();
            mStatus = StatusButtonS.get(0).getTag().toString();
            mTypeName = fangButtonS.get(0).getText().toString();
            mStatusName = StatusButtonS.get(0).getText().toString();
        });
        tvSure.setOnClickListener(view -> {
            dialogListener.onClick(view, mTypeName, mStatusName, mType, mStatus);
            dismiss();
        });

    }

    @Override
    protected void setViews() {
        setBackground();
        setLayout();
    }

    /**
     * 强制取消背景，保持有透明
     */
    private void setBackground() {
//        getDialog().getWindow().setBackgroundDrawable(new ColorDrawable()); // 去除dialog的背景，即透明
//        getDialog().getWindow().setBackgroundDrawable(new ColorDrawable(0xffffffff)); // 设置白色背景
        getDialog().getWindow().setBackgroundDrawableResource(R.drawable.white_bg); // 设置背景
    }


    /**
     * 也可通过setLayout来设置：
     * getDialog().getWindow().setLayout(dm.widthPixels, getDialog().getWindow().getAttributes().height);
     */
    private void setLayout() {
        Window window = getDialog().getWindow();
        final WindowManager.LayoutParams lp = window.getAttributes();

        // 强制宽高
//        int padding = DisplayUtils.dp2px(0);
        lp.width = getScreenWidth(getActivity());
//        lp.height = getResources().getDimensionPixelOffset(R.dimen.dialog_height);

        lp.gravity = Gravity.BOTTOM; // 设置展示的位置
//        lp.y = getResources().getDimensionPixelOffset(R.dimen.join_shop_dialog_margin);
        window.setAttributes(lp);
    }

    public static int getScreenWidth(Activity activity) {
        final DisplayMetrics dm = new DisplayMetrics();
        activity.getWindowManager().getDefaultDisplay().getMetrics(dm);
        return dm.widthPixels;
    }


    @Override
    public void onDestroyView() {
        super.onDestroyView();
    }

    /**
     * 监听输入框内容
     */
    public interface OnDialogClickListener {
        /**
         * Called when menu_text_color view has been clicked.
         *
         * @param v The view that was clicked.
         */
        void onClick(View v, String typeName, String statusName, String typeNumber, String status);
    }


}
